package com.sos.core.game.requests
{
	import com.sos.comm.CallFactory;
	import com.sos.core.game.GamePhase;
	import com.sos.utils.StringUtils;

	/**
	 * Static factory for Gamerequest creation: uses an internal counter to generate the unique
	 * @author ltavecchia
	 */	
	public class GameRequestFactory
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// counters limits definition
		private static const	COUNTER_MIN:int		= 0;
		private static const	COUNTER_MAX:int		= 999999;
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private static var _counter:uint = 0;

		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		private static function nextCounter():int
		{
			var next:int = _counter + 1;
			if (next >= COUNTER_MAX)
			{
				next = COUNTER_MIN;
			}
			return next;
		}
		
		private static function nextId():String
		{
			// Local
			// var nextId:String = "";			
			// Increment the counter
			_counter = nextCounter();
			
			// LT_TODO: algoritmo per la generazione dell'id, per ora solo id contatore formattato
			
			// Returns the generated if
			return StringUtils.padLeft(_counter.toString(), "0", 5);
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * 
		 * @param type
		 * @return 
		 */		
		public static function create(phase:GamePhase, params:GameRequestParams):GameRequest
		{
			// Output request
			var request:GameRequest;
			// Create the right cation for type
			switch(phase)
			{
				/*
				case GamePhase.SUBSTITUTE:			request = new GameRequestSubstitute(CallFactory.CALL_GAME_SUBSTITUTE, nextId());					break;
				case GamePhase.RESOLVE_SUBSTITUTE:	request = new GameRequestResolveSubstitution(CallFactory.CALL_GAME_RESOLVE_SUBSTITUTE, nextId());	break;
				case GamePhase.DISCARD:				request = new GameRequestDiscard(CallFactory.CALL_GAME_DISCARD, nextId()); 							break;
				case GamePhase.RESOLVE_DISCARD:		request = new GameRequestResolveDiscard(CallFactory.CALL_GAME_RESOLVE_DISCARD, nextId()); 			break;
				case GamePhase.PLAY:				request = new GameRequestPlay(CallFactory.CALL_GAME_PLAY, nextId()); 								break;
				case GamePhase.RESOLVE_PLAY:		request = new GameRequestResolvePlay(CallFactory.CALL_GAME_RESOLVE_PLAY, nextId()); 				break;
				case GamePhase.RESYNC:				request = new GameRequestResync(CallFactory.CALL_GAME_RESYNC, nextId()); 							break;
				*/
				
				case GamePhase.SUBSTITUTE:			request = new GameRequestSubstitute(CallFactory.CALL_GAME_CONTROL, 			nextId());	break;
				case GamePhase.RESOLVE_SUBSTITUTE:	request = new GameRequestResolveSubstitution(CallFactory.CALL_GAME_CONTROL, nextId());	break;
				case GamePhase.DISCARD:				request = new GameRequestDiscard(CallFactory.CALL_GAME_CONTROL, 			nextId()); 	break;
				case GamePhase.RESOLVE_DISCARD:		request = new GameRequestResolveDiscard(CallFactory.CALL_GAME_CONTROL, 		nextId()); 	break;
				case GamePhase.PLAY:				request = new GameRequestPlay(CallFactory.CALL_GAME_CONTROL, 				nextId()); 	break;
				case GamePhase.RESOLVE_PLAY:		request = new GameRequestResolvePlay(CallFactory.CALL_GAME_CONTROL, 		nextId()); 	break;
				case GamePhase.RESYNC:				request = new GameRequestResync(CallFactory.CALL_GAME_CONTROL, 				nextId()); 	break;
				
				default: throw new Error("GameRequestFactory.create() erorr: Unknown game request type '" + phase.name + "'");

			}
			// Initializes the request
			request.init(params);
			// Returns the new request
			return request;
		}
		
		/**
		 * 
		 * @param type
		 * @param params
		 * @return 
		 */
		public static function createParams(phase:GamePhase):GameRequestParams
		{
			// Output request
			var params:GameRequestParams;
			// Create the right cation for type
			switch(phase)
			{
				case GamePhase.SUBSTITUTE:			params = new GameRequestSubstituteParams();	break;
				case GamePhase.RESOLVE_SUBSTITUTE:	params = new GameRequestParams();			break;
				case GamePhase.DISCARD:				params = new GameRequestDiscardParams(); 	break;
				case GamePhase.RESOLVE_DISCARD:		params = new GameRequestParams(); 			break;
				case GamePhase.PLAY:				params = new GameRequestPlayParams(); 		break;
				case GamePhase.RESOLVE_PLAY:		params = new GameRequestParams(); 			break;
				case GamePhase.RESYNC:				params = new GameRequestParams(); 			break;
				
				default: throw new Error("GameRequestFactory.createParams() erorr: Unknown game request type '" + phase.name + "'");
					
			}
			return params;
		}
		
	}
}